local yitu = fk.CreateSkill{
  name = "hanqing__yitu",
}

Fk:loadTranslationTable{
  ["hanqing__yitu"] = "移图",
  [":hanqing__yitu"] = "一名角色发动〖攀云〗/〖崩坏〗时，若与上次发动的选择项相同，你可以选择一项：1.移动场上一个〖崩坏〗/〖攀云〗；"..
  "2.移动场上一张武器/防具牌。",

  ["hanqing__yitu1"] = "移动一名角色的“%arg”",
  ["hanqing__yitu2"] = "移动场上一张%arg",
  ["hanqing__yitu_active"] = "移图",
  ["#hanqing__yitu1-move"] = "移图：选择两名角色，将前者的“%arg”移给后者",
  ["#hanqing__yitu2-move"] = "移图：请移动场上一张%arg",

  ["$hanqing__yitu1"] = "魏国献城，复围大梁无益，吾其退也。",
  ["$hanqing__yitu2"] = "攻齐不成，借赵兵而增祸，吾不取也。",
}

yitu:addEffect(fk.AfterSkillEffect, {
  anim_type = "control",
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(yitu.name) and player.room:getBanner(yitu.name) then
      player.room:setBanner(yitu.name, nil)
      return data.skill.name == "hanqing__panyun" or data.skill.name == "hanqing__benghuai"
    end
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    local choices = {}
    local name, type
    if data.skill.name == "hanqing__panyun" then
      name = "hanqing__benghuai"
      type = Card.SubtypeWeapon
    else
      name = "hanqing__panyun"
      type = Card.SubtypeArmor
    end
    if table.find(room.alive_players, function (p)
      return p:hasSkill(name, true) and
        table.find(room:getOtherPlayers(p, false), function (q)
          return not q:hasSkill(name, true)
        end) ~= nil
    end) then
      table.insert(choices, "hanqing__yitu1:::"..name)
    end
    local excludeIds = {}
    for _, p in ipairs(room.alive_players) do
      for _, id in ipairs(p:getCardIds("e")) do
        if Fk:getCardById(id).sub_type ~= type then
          table.insert(excludeIds, id)
        end
      end
    end
    local subtype_string_table = {
      [Card.SubtypeWeapon] = "weapon",
      [Card.SubtypeArmor] = "armor",
    }
    if #room:canMoveCardInBoard("e", room.alive_players, excludeIds) > 0 then
      table.insert(choices, "hanqing__yitu2:::"..subtype_string_table[type])
    end
    table.insert(choices, "Cancel")
    local choice = room:askToChoice(player, {
      choices = choices,
      skill_name = yitu.name,
    })
    if choice:startsWith("hanqing__yitu1") then
      local success, dat = room:askToUseActiveSkill(player, {
        skill_name = "#hanqing__yitu_active",
        prompt = "#hanqing__yitu1-move:::"..name,
        extra_data = {
          skill_name = name,
        },
      })
      if success and dat then
        event:setCostData(self, {tos = dat.targets, choice = 1, extra_data = name})
        return true
      end
    elseif choice:startsWith("hanqing__yitu2") then
      local targets = room:askToChooseToMoveCardInBoard(player, {
        prompt = "#hanqing__yitu2-move:::"..subtype_string_table[type],
        skill_name = yitu.name,
        cancelable = true,
        flag = "e",
        exclude_ids = excludeIds,
      })
      if #targets == 2 then
        event:setCostData(self, {tos = targets, choice = 2, extra_data = excludeIds})
        return true
      end
    end
  end,
  on_use = function(self, event, target, player)
    local room = player.room
    local targets = event:getCostData(self).tos
    if event:getCostData(self).choice == 1 then
      local name = event:getCostData(self).extra_data
      room:handleAddLoseSkills(targets[1], "-"..name, nil, true, false)
      room:handleAddLoseSkills(targets[2], name, nil, true, false)
    else
      local excludeIds = event:getCostData(self).extra_data
      room:askToMoveCardInBoard(player, {
        target_one = targets[1],
        target_two = targets[2],
        skill_name = yitu.name,
        flag = "e",
        exclude_ids = excludeIds,
      })
    end
  end,
})

return yitu
